//
//  LTAvatarViewController.m
//  LTChat
//
//  Created by 俊松何 on 2017/7/28.
//  Copyright © 2017年 俊松何. All rights reserved.
//

#import "LTAvatarViewController.h"
#import "LTSelectPhotoView.h"


@interface LTAvatarViewController ()<LTSelectPhotoViewDelegate,UIImagePickerControllerDelegate>

@property(nonatomic,strong) UIImageView *imageView;
@property(nonatomic,strong) LTSelectPhotoView *selectphotoView;


@property(nonatomic,strong) UIImage *selectImage;

@end

@implementation LTAvatarViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}


-(void)yd_layoutNavigation
{
    self.title = NSLTLocalizedString(@"personInfo.avatar", nil);

    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
//    but.size = CGSizeMake(44, 44);
    but.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
    
    [but setImage:[UIImage imageNamed:@"more_set_talk"] forState:UIControlStateNormal];
    [but addTarget:self action:@selector(moreSet) forControlEvents:UIControlEventTouchUpInside];
    but.titleLabel.font = [UIFont systemFontOfSize:30];
    [but sizeToFit];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:but];
//    self.navigationItem.rightMargin = -3;


    
}

-(void)yd_addSubviews
{
    _imageView = [UIImageView new];
    [self.view addSubview:_imageView];
    
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX).offset(0);
        make.centerY.mas_equalTo(self.view.mas_centerY).offset(0);
        make.height.mas_equalTo(WIDTH);
        make.width.mas_equalTo(WIDTH);
    }];
    
    NSArray *urls = [[LTClient sharedClient].currentUser.avatar componentsSeparatedByString:@"?"];
    
    if(urls.count >= 1)
    {
        NSURL *url = [NSURL URLWithString:urls.firstObject];
        [_imageView sd_setImageWithURL:url placeholderImage:defaultImgae];
    }
    self.view.backgroundColor = [UIColor blackColor];

}


-(LTSelectPhotoView *)selectphotoView
{
    if(_selectphotoView == nil)
    {
        _selectphotoView = [LTSelectPhotoView ltselectView];
        _selectphotoView.delegate = self;
    }
    return _selectphotoView;
}


-(void)selectPhotoViewSelectPhoto:(LTSelectPhotoView *)view
{
    [self pushTZImagePickerController];
}

-(void)talkPhotoViewSelectPhoto:(LTSelectPhotoView *)view
{
    [self takePhoto];
    
}


-(void)moreSet
{
    [self.selectphotoView show];

}



#pragma mark - 照相
- (void)takePhoto {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    
}

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *originalimage = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *editImage = [info objectForKey:UIImagePickerControllerEditedImage];
    if(editImage != nil)
    {
        self.selectImage = editImage;
    }else
    {
        self.selectImage = originalimage;
    }
    
    self.imageView.image = self.selectImage;

    [MBProgressHUD showProcessHud:NSLTLocalizedString(@"main.loding", nil) toView:self.view];
    NSData *data = UIImageJPEGRepresentation(self.selectImage, 0.1);
    [[LTHTTPTool shareTool] uploadImageData:data progress:^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
        
    } success:^(NSString *fileUrl, LTFileTokenModel *model) {
        
        NSDictionary *param = @{@"avatar":fileUrl};
        
        [[MTHttpTool shareTool] postPath:@"/user/update_info" parameters:param success:^(MTBaseData *datamodel) {
            [MBProgressHUD hideHUDForView:self.view];
            [MBProgressHUD showSuccess:@"修改成功" toView:nil];
            [LTClient sharedClient].currentUser.avatar = fileUrl;
            MTUserModel *model = [MTUserModel currentUser];
            model.avatar = fileUrl;
            [MTUserModel saveCurrentUser:model];
            [[NSNotificationCenter defaultCenter] postNotificationName:UserInfoChange object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        } failer:^(MTError *error) {
            [MBProgressHUD hideHUDForView:self.view];
            [MBProgressHUD showError:error.errorDescription];
        }];

        
    } failer:^(LTError *lterror) {
        [MBProgressHUD hideHUDForView:self.view];
    }];
    
}

#pragma mark - 选择图片
- (void)pushTZImagePickerController {
   
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:^{
        
    }];

    
}


@end
